package follow

import (
	"context"
	"fmt"
	"gitee.com/mootok/mootok/src/constant/config"
	"gitee.com/mootok/mootok/src/models"
	redis2 "gitee.com/mootok/mootok/src/storage/redis"
	redis3 "github.com/redis/go-redis/v9"
	"github.com/sirupsen/logrus"
)

var followIdScoreListPrefix = config.EnvConfig.RedisPrefix + "follow_id_score_list:"
var fansIdScoreListPrefix = config.EnvConfig.RedisPrefix + "fans_id_score_list:"
var followUserCountPrefix = config.EnvConfig.RedisPrefix + "follow_user_count:"
var fansUserCountPrefix = config.EnvConfig.RedisPrefix + "follow_user_count:"

func genFollowIdScoreListKey(fansId int) string {
	return fmt.Sprintf("%s%d", followIdScoreListPrefix, fansId)
}

func genFansIdScoreListKey(followId int) string {
	return fmt.Sprintf("%s%d", fansIdScoreListPrefix, followId)
}

func genFollowUserCountKey(fansId int) string {
	return fmt.Sprintf("%s%d", followUserCountPrefix, fansId)
}

func genFansUserCountKey(followId int) string {
	return fmt.Sprintf("%s%d", fansUserCountPrefix, followId)
}

func DelCountCacheAfterFollow(ctx context.Context, followRelation models.FollowRelation, logger *logrus.Entry) (err error) {

	//删除关注
	err = redis2.Client.Del(ctx, genFollowUserCountKey(followRelation.FansId)).Err()
	if err != nil {
		logger.Errorf("removeFollowOrFansCountCache from redis failed %v", err)
	}
	err = redis2.Client.Del(ctx, genFansUserCountKey(followRelation.FollowId)).Err()
	if err != nil {
		logger.Errorf("removeFollowOrFansCountCache from redis failed %v", err)
	}
	return
}

func DelCountCacheAfterUnFollow(ctx context.Context, followRelation models.FollowRelation, logger *logrus.Entry) (err error) {

	//删除粉丝数量
	err = redis2.Client.Del(ctx, genFansUserCountKey(followRelation.FollowId)).Err()
	if err != nil {
		logger.Errorf("removeFollowOrFansCountCache from redis failed %v", err)
	}
	err = redis2.Client.Del(ctx, genFollowUserCountKey(followRelation.FansId)).Err()
	if err != nil {
		logger.Errorf("removeFollowOrFansCountCache from redis failed %v", err)
	}

	return
}

func updateRelationCacheAfterFollow(ctx context.Context, followRelation models.FollowRelation, logger *logrus.Entry) (err error) {
	score := float64(followRelation.CreatedAt.Unix())
	//我多个关注
	err = redis2.Client.ZAdd(ctx, genFollowIdScoreListKey(followRelation.FansId), redis3.Z{
		Member: followRelation.FollowId,
		Score:  score,
	}).Err()
	if err != nil {
		logger.Errorf("updateFolloweeIdListCache from redis failed %v", err)
	}
	//对面多个粉丝
	err = redis2.Client.ZAdd(ctx, genFansIdScoreListKey(followRelation.FollowId), redis3.Z{
		Member: followRelation.FansId,
		Score:  score,
	}).Err()
	if err != nil {
		logger.Errorf("updateFolloweeIdListCache from redis failed %v", err)
	}
	return
}

func updateRelationCacheAfterUnFollow(ctx context.Context, followRelation models.FollowRelation, logger *logrus.Entry) (err error) {
	//我少一个关注
	err = redis2.Client.ZRem(ctx, genFollowIdScoreListKey(followRelation.FansId), followRelation.FollowId).Err()
	if err != nil {
		logger.Errorf("updateFolloweeIdListCache from redis failed %v", err)
	}
	//对面少一个粉丝
	err = redis2.Client.ZRem(ctx, genFansIdScoreListKey(followRelation.FollowId), followRelation.FansId).Err()
	if err != nil {
		logger.Errorf("updateFolloweeIdListCache from redis failed %v", err)
	}
	return
}
